package com.aier.admin.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.admin.entity.BuyMessage;

public class BuyMessageRowMapper implements RowMapper<BuyMessage> {

    @SuppressWarnings("unused")
    private int type;

    public BuyMessageRowMapper() {
    }

    public BuyMessageRowMapper(int type) {
        this.type = type;
    }

    @Override
    public BuyMessage mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseAdminUserInfo(new BuyMessage(), rs);
    }

    public static BuyMessage parseAdminUserInfo(BuyMessage f, ResultSet rs)
        throws SQLException {
        return parseAdminUserInfo(f, rs, null);
    }

    public static BuyMessage parseAdminUserInfo(BuyMessage f, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            f.setBid(rs.getLong(su + "bid"));
        } catch (SQLException e) {
            f.setBid(null);
        }
        try {
            f.setSid(rs.getLong(su + "sid"));
        } catch (SQLException e) {
            f.setSid(null);
        }
        try {
            f.setPrice(rs.getDouble(su + "price"));
        } catch (SQLException e) {
            f.setPrice(0);
        }
        try {
            f.setCount(rs.getInt(su + "count"));
        } catch (SQLException e) {
            f.setCount(0);
        }
        try {
            f.setTotal(rs.getDouble(su + "total"));
        } catch (SQLException e) {
            f.setTotal(0);
        }
        try {
            f.setBdate(rs.getTimestamp(su + "bdate"));
        } catch (SQLException e) {
            f.setBdate(null);
        }
        try {
            f.setName(rs.getString(su + "name"));
        } catch (SQLException e) {
            f.setName(null);
        }
        return f;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, BuyMessage f) {
        par.put("bid", f.getBid());
        par.put("sid", f.getSid());
        par.put("price", f.getPrice());
        par.put("count", f.getCount());
        par.put("total", f.getTotal());
        par.put("bdate", f.getBdate());
        par.put("name", f.getName());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args,
            BuyMessage f) {
        args.put("bid", f.getBid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final BuyMessage f) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(f),
                        new String[] { "bid" });
                parsePS(ps, f);
                return ps;
            }

        };
    }
    
    private static String parseSQL(BuyMessage f) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into buy_message_record(");
        int count = 0;
        if (null != f.getSid()) {
            sql.append("sid,");
            count++;
        }
        sql.append("price,");
        count++;
        sql.append("count,");
        count++;
        sql.append("total,");
        count++;
        if (null != f.getBdate()) {
            sql.append("bdate,");
            count++;
        }
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }


    private static void parsePS(PreparedStatement ps, BuyMessage f)
        throws SQLException {
        int count = 1;
        if (null != f.getBid()) {
            ps.setLong(count++, f.getBid());
        }

        if (null != f.getSid()) {
            ps.setLong(count++, f.getSid());
        }
        ps.setDouble(count++, f.getPrice());
        ps.setInt(count++, f.getCount());
        ps.setDouble(count++, f.getTotal());
        if (null != f.getBdate()) {
            ps.setTimestamp(count++, f.getBdate());
        }
        if (null != f.getName()) {
            ps.setString(count++, f.getName());
        }
    }

}
